var LevelUpSelectButton = require("LevelUpSelectButton");
var PlayerStatUpgradeDisplay = require("PlayerStatUpgradeDisplay");
//var PlayerStatController = require("PlayerStatController");

var UIController = cc.Class({
    extends: cc.Component,

    properties: {
        expLevelBar: cc.ProgressBar,
        expLevelLabel: cc.Label,

        levelUpButtons: [LevelUpSelectButton],
        levelUpPanel: cc.Node,

        coinLabel: cc.Label,

        moveSpeedUpgradeDisplay: PlayerStatUpgradeDisplay,
        healthUpgradeDisplay: PlayerStatUpgradeDisplay,
        pickUpRangeUpgradeDisplay: PlayerStatUpgradeDisplay,
        maxWeaponsUpgradeDisplay: PlayerStatUpgradeDisplay,

        timeLabel: cc.Label,

        levelEndScreen: cc.Node,
        endTimeLabel: cc.Label,

        pauseScreen: cc.Node,
    },

    statics: {
        instance: null,
    },
    ctor () {
        UIController.instance = this;
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
    },

    onDestroy () {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
    },

    onKeyDown (event) {
        if (event.keyCode == cc.macro.KEY.escape) {
            this.pauseUnpause();
        }
    },

    updateExp (currentExp, levelExp, currentLevel) {
        this.expLevelBar.progress = currentExp / levelExp;

        this.expLevelLabel.string = "Level: " + currentLevel;
    },

	skipSelectLevelUp () {
		this.levelUpPanel.active = false;
		cc.director.resume();
	},

    updateCoins (currentCoins) {
        this.coinLabel.string = "Coin: " + currentCoins;
    },

    purchaseMoveSpeed () {
        require("PlayerStatController").instance.purchaseMoveSpeed();
        this.skipSelectLevelUp();
    },

    purchaseHealth () {
        require("PlayerStatController").instance.purchaseHealth();
        this.skipSelectLevelUp();
    },

    purchasePickUpRange () {
        require("PlayerStatController").instance.purchasePickUpRange();
        this.skipSelectLevelUp();
    },

    purchaseMaxWeapons () {
        require("PlayerStatController").instance.purchaseMaxWeapons();
        this.skipSelectLevelUp();
    },

    updateTimer (time) {
        let timer = Math.round(time);
        let minutes = Math.floor(timer / 60);
        let seconds = timer % 60;
        if (seconds < 10) {
            seconds = "0" + seconds;
        }
        this.timeLabel.string = minutes + ":" + seconds;
    },

    goToMainMenu () {
        //返回主菜单，后面写
        cc.director.resume();
        cc.director.loadScene("MainMenu");
    },

    restart () {
        cc.director.resume();
        cc.director.loadScene("Main");
    },

    quitGame () {
        cc.game.end();
    },

    pauseUnpause () {
        if (this.pauseScreen.active == false) {
            this.pauseScreen.active = true;
            cc.director.pause();
        } else {
            this.pauseScreen.active = false;
            if (this.levelUpPanel.active == false) {
                cc.director.resume();
            }
        }
    },
});

module.exports = UIController;
